/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.rxjava.circle;

import rx.Observable;
import rx.Scheduler;
import rx.functions.Action1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

import java.util.Optional;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.rxjava.circle
 * @author:LiBo/Alex
 * @create-date:2019-09-02 15:43
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class Sequence {


    /**
     * start 默认值
     */
    private static final int DEFAULT_START_INDEX = 0;

    /**
     * end 默认值
     */
    private static final int DEFAULT_END_INDEX = Integer.MAX_VALUE;


    /**
     *
     * @param start
     * @param end
     * @param initialValue
     * @param input
     * @return
     */
    public static void interate(int start, int end, Scheduler scheduler, Object initialValue, Func2 input, Action1 output){
        scheduler = Optional.ofNullable(scheduler).orElse(Schedulers.io());
        Observable.range(start,end,scheduler).scan(initialValue,input).toBlocking().forEach(output);
    }

    /**
     *
     * @param count
     * @param scheduler
     * @param initialValue
     * @param input
     * @param output
     */
    public static void interate(int count, Scheduler scheduler, Object initialValue, Func2 input,Action1 output){
        interate(DEFAULT_START_INDEX,count,scheduler,initialValue,input,output);
    }

    /**
     *
     * @param count
     * @param initialValue
     * @param input
     * @param output
     */
    public static void interate(int count, Object initialValue, Func2 input,Action1 output){
        interate(count,null,initialValue,input,output);
    }

    /**
     *
     * @param initialValue
     * @param input
     * @param output
     */
    public static void interate(Object initialValue, Func2 input,Action1 output){
        interate(DEFAULT_START_INDEX,DEFAULT_END_INDEX,null,initialValue,input,output);
    }



    public static void main(String[] args){
        interate(10," - ",(a,b)->{
            return a.toString()+b;
        },(c)->{
            System.out.println(c);
        });
    }
}
